import {
  ArgumentsHost,
  Catch,
  ExceptionFilter,
  HttpException,
} from '@nestjs/common';
import { Response } from 'express';

@Catch(HttpException)
export class AppExceptionFilter implements ExceptionFilter {
  catch(exception: HttpException, host: ArgumentsHost) {
    const ctx = host.switchToHttp();
    const res = ctx.getResponse<Response>();
    const req = ctx.getRequest<Request>();
    const status = exception.getStatus();

    console.group('appExceptionFilter in');
    console.log('in req', req.url);
    console.log('in res', JSON.stringify(res.getHeaders()));
    console.groupEnd();

    console.log('her');
    res.status(status).json({
      from: 'app-exception',
      statusCode: status,
      timestamp: new Date().toISOString(),
      path: req.url,
    });
  }
}
